package com.ad.pet.app.shopcar.confirm;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.CommList;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.account.Banlance;
import com.ad.pet.models.remote.account.UserBuyReturn;
import com.ad.pet.models.remote.account.order.PayCompleted;
import com.ad.pet.models.remote.account.order.SignCheck;
import com.ad.pet.models.remote.cart.BigShipingw;
import com.ad.pet.models.remote.cart.Coupons;
import com.ad.pet.models.remote.mall.Counpous;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by SEELE on 2018/5/2.
 */
@PerActivity
public class ConfirmOrderPresenter extends AbsBasePresenter implements ConfirmOrderContracts.Presenter{

    private ApiService mApiService;
    private ConfirmOrderContracts.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public ConfirmOrderPresenter(ApiService apiService,
                                 Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onCreate(ConfirmOrderContracts.View view) {
        this.mView = view;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }


    @Override
    public void getCoupons(String state,String page,String pageSize) {
        mRxDisposable.add(mApiService.getCouponList(state,page,pageSize)
                .subscribe(new AppSubscriber<CommList<List<Counpous>>>(mView) {
                    @Override
                    public void next(CommList<List<Counpous>> coupons) {
                        mView.getCouponsSuccess(coupons.getList());
                    }
                }));
    }

    @Override
    public void checkCoupons(String code) {
        mRxDisposable.add(mApiService.checkCouponCode(code)
                .subscribe(new AppSubscriber<Coupons>(mView) {
                    @Override
                    public void next(Coupons coupons) {
                        mView.checkCouponsSuccess(coupons);
                    }
                }));
    }

    @Override
    public void get_user_balance() {
        mRxDisposable.add(mApiService.getUserBalance()
                .subscribe(new AppSubscriber<Banlance>(mView) {
                    @Override
                    public void next(Banlance shop) {

                        mView.getUserBanceSuccess(shop);

                    }
                }));
    }

    @Override
    public void confirmOrder(String cartIds, String aids, String sid, String banlance, String redEv, String freight,String cpid) {
        mRxDisposable.add(mApiService.confirm(cartIds, aids, sid, banlance, redEv, freight,cpid)
                .subscribe(new AppSubscriber<PayCompleted>(mView) {
                    @Override
                    public void next(PayCompleted shop) {

                        mView.confirmOrderSuccess(shop);

                    }
                }));
    }

    @Override
    public void getShipingWays(String number,String price) {
        mRxDisposable.add(mApiService.getShippingWays(number,price)
                .subscribe(new AppSubscriber<BigShipingw>(mView) {
                    @Override
                    public void next(BigShipingw shop) {

                        mView.getShipingWaysSuccess(shop);

                    }
                }));
    }

    @Override
    public void paymentBalance(String order_sn, String balance_sign, String balance, String balance_red_envele) {
        mRxDisposable.add(mApiService.paymentBalance(order_sn, balance_sign, balance, balance_red_envele)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User shop) {

                        mView.paymentBalanceSuccess(Integer.valueOf(shop.getTo_pay()));

                    }
                }));
    }

    @Override
    public void checkIsSign() {
        mRxDisposable.add(mApiService.checkIsSign()
                .subscribe(new AppSubscriber<SignCheck>(mView) {
                    @Override
                    public void next(SignCheck shop) {
                        mView.checkIsSignSuccess(shop);

                    }
                }));
    }

    @Override
    public void daySign() {
        mRxDisposable.add(mApiService.DaySign()
                .subscribe(new AppSubscriber<SignCheck>(mView) {
                    @Override
                    public void next(SignCheck shop) {
                        mView.daySignSuccess(shop);

                    }
                }));
    }

    @Override
    public void shareH5Url() {
        mRxDisposable.add(mApiService.app_share_h5_url()
                .subscribe(new AppSubscriber<SignCheck>(mView) {
                    @Override
                    public void next(SignCheck shop) {
                        mView.sharH5UrlSuccess(shop);

                    }
                }));
    }

    @Override
    public void get_user_buy_return() {
        mRxDisposable.add(mApiService.get_user_buy_return()
                .subscribe(new AppSubscriber<UserBuyReturn>(mView) {
                    @Override
                    public void next(UserBuyReturn shop) {
                        mView.get_user_buy_return_success(shop);

                    }
                }));
    }

}
